﻿/*
* ibio-develop
* Sun Mar 09 14:05:07 2008
* ===========被扩展的影片剪辑需要包含===========
* mc_loader：加载mc的容器
* mc_bar：显示mc加载的进度
* txt_loaded：显示mc加载的百分比
*/

class application.extend.MCContentLoader extends MovieClip{
	private var m_mcLoader:MovieClipLoader;
	private var m_w:Number = 100;
	private var m_h:Number = 100;
	private var m_isScale:Boolean = false;
	//
	private var mc_loader:MovieClip;
	private var mc_bar:MovieClip;
	private var txt_loaded:TextField;
	
	function MCContentLoader(){
		m_mcLoader = new MovieClipLoader();
		m_mcLoader.addListener(this);
		this.mc_loader._visible = false;
		this.mc_bar._xscale = 0;
		m_w = this.mc_loader._width;
		m_h = this.mc_loader._height;
	}
	
	/**
	* 设置加载的地址
	*/
	public function load(url):Void{
		//这里新建立一个mc的原因是：清除之前加载图片的宽度和高度
		this.mc_loader.createEmptyMovieClip("mc", 1);
		m_mcLoader.loadClip(url , this.mc_loader.mc);
	}
	
	/**
	* 设置加载完成后的大小和是否保持宽高比
	* @param w: 
	* @param h:
	* @param scaleContent:true为自动等比例缩放加载内容的尺寸到(w*h)
	*/
	public function setSize(w:Number, h:Number, scaleContent:Boolean):Void{
		m_w = w;
		m_h = h;
		m_isScale = scaleContent;
	}
	
	/**
	* 设置否保持宽高比
	* @param maintainAspectRatio:true为自动等比例缩放加载内容的尺寸到(w*h)
	*/
	public function set maintainAspectRatio(isMaintain):Void{
		m_isScale = isMaintain;
	}
	
	private function onLoadInit():Void{
		this.mc_loader._visible = true;
		this.mc_bar._visible = false;
		//this.txt_loaded._visible = false;
		//是否等比例缩放图片
		if(m_isScale){
			changeMcSize();
		}else{
			this.mc_loader.mc._width = m_w;
			this.mc_loader.mc._height = m_h;
		}
	}
	
	private function onLoadProgress(target, bytesLoaded, bytesTotal):Void{
		var percent = Math.round(bytesLoaded/bytesTotal*100);
		this.mc_bar._xscale = percent;
		//this.txt_loaded.text = percent + "%";
	}
	
	private function changeMcSize():Void{
		var width = this.mc_loader.mc._width;
		var height = this.mc_loader.mc._height;
		var ratio = 1;
		//flag是居中方式; v:垂直
		var flag = "";
		
		var pW = m_w/width;
		var pH = m_h/height;
		
		/*如果宽度的缩放比 > 高度的缩放比, 则使用高度的缩放比
		* 否则使用宽度的缩放比, 把照片缩小到 (m_w * m_h) 以内
		*/
		if (pW > pH){	
			ratio = pH;
		}else{
			ratio = pW;
			flag = "v";
		}
		this.mc_loader.mc._width = width*ratio;
		this.mc_loader.mc._height = height*ratio;
		changeMcPosition(flag);
	}
	
	/**
	* 使加载完成后图片的相对位置居中
	*/
	private function changeMcPosition(flag:String):Void{
		if(flag == "v"){
			this.mc_loader.mc._y = (m_h - this.mc_loader.mc._height)/2;
		}else{
			this.mc_loader.mc._x = (m_w - this.mc_loader.mc._width)/2;
		}
	}
}